package cn.net.xyan.blossom.kotlin

import com.github.stuxuhai.jpinyin.PinyinFormat
import com.github.stuxuhai.jpinyin.PinyinHelper
import org.apache.commons.text.CharacterPredicate
import org.apache.commons.text.RandomStringGenerator

const val ChineseCharBegin: Char = 0x4e00.toChar()
const val ChineseCharEnd: Char = 0x9FFF.toChar()



fun randomString(length:Int,accept:String = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890~!@#$%&*()-=+{}[]/<>?"):String{
    val generator = RandomStringGenerator.Builder().filteredBy(
            CharacterPredicate{
                c ->
                if (c > Char.MAX_SURROGATE.toInt() ) false
                else
                    accept.contains(c.toChar())
            }
    ).build()

    return generator.generate(length)
}

val Char.isChinese:Boolean get(){
    return this in (ChineseCharBegin + 1)..(ChineseCharEnd - 1)
}

val String.pinyin:String get() {
    return PinyinHelper.convertToPinyinString(this, "", PinyinFormat.WITHOUT_TONE)
}